/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} sum
 * @return {number[][]}
 */
var pathSum = function(root, sum) {
    if(sum===0&&!root)return [];
    let res = [];

    helper(root, sum, res, "");

    return res;


};

function helper(root, sum, res, cur) {
    if(!root)return false;
    sum = sum - root.val;
    cur = cur+root.val+",";
    if(!root.left&&!root.right){
        if(sum!==0)return false;
        cur = cur.split(",");
        cur.pop();
        cur = cur.map(v=>parseInt(v,10));
        res.push(cur);
        return true;
    }else{
        helper(root.left, sum, res, cur);
        helper(root.right, sum, res, cur);
    }
}